import { ElButton, ElSpace } from 'element-plus'
import { defineComponent } from 'vue'
import { VxeGrid } from 'vxe-table'

import type { IPostPageItem } from '@/apis'
import { StatusTag } from '@/components'
import { usePageHeight } from '@/hooks'

import { PostDialog } from './components'
import { useGrid } from './hooks'

export default defineComponent({
  setup() {
    const { gridExtHook, ...gridRestHook } = useGrid()
    const { pageHeightPx } = usePageHeight()

    return {
      pageHeightPx,
      gridRef: gridExtHook.gridRef,
      ...gridRestHook
    }
  },
  render() {
    return (
      <div
        style={{
          height: this.pageHeightPx
        }}
      >
        <VxeGrid ref={(e: any) => (this.gridRef = e)} {...this.gridConfig}>
          {{
            toolbar_buttons: () => (
              <ElSpace>
                <ElButton
                  v-permission="'system:post:add'"
                  type="primary"
                  plain
                  onClick={() => this.handleShowAdd()}
                >
                  新增
                </ElButton>
                <ElButton
                  v-permission="'system:post:remove'"
                  type="danger"
                  plain
                  disabled={this.selectedIds.size === 0}
                  onClick={this.handleDeleteList}
                >
                  删除
                </ElButton>
                <ElButton
                  v-permission="'system:post:export'"
                  type="primary"
                  plain
                  disabled={!this.canExport}
                  onClick={this.handleExport}
                >
                  导出
                </ElButton>
              </ElSpace>
            ),
            columns_status: ({ row }: { row: IPostPageItem }) => (
              <StatusTag value={row.status} />
            ),
            columns_operation: ({ row }: { row: IPostPageItem }) => (
              <ElSpace>
                <ElButton
                  v-permission="'system:post:edit'"
                  type="primary"
                  size="small"
                  link
                  onClick={() => this.handleShowEdit(row)}
                >
                  修改
                </ElButton>
                <ElButton
                  v-permission="'system:post:remove'"
                  type="danger"
                  size="small"
                  link
                  onClick={() => this.handleDelete(row.id)}
                >
                  删除
                </ElButton>
              </ElSpace>
            )
          }}
        </VxeGrid>
        {/* 新增/编辑操作  */}
        <PostDialog
          ref={(e: any) => (this.postDialogRef = e)}
          statusOptions={this.statusOptions}
          onSuccess={this.handleRefresh}
        />
      </div>
    )
  }
})
